﻿$(function () {
    /*自适应布局*/
    if ($("#bodymain").length > 0) {
        reLayoutFun("#bodymain", window, 10);
    }

    /* 更换主题 */
    if ($.cookie('eTheme')) {
        changeThemeFun($.cookie('eTheme'));
    }
})


/*
* 设置边框，自适应布局
* @id:    {string id}      布局ID
* @win:   {Object window}  浏览器窗口
* @width: {int width}      边框宽度
*/
function reLayoutFun(id, win, width) {
    //初始时自适应
    var sHeight = $(win).height() - width;
    $(id).css({ "height": sHeight }); //设置高度
    $(id).layout(); //重新布局

    // 改变窗体大小时自适应
    $(win).resize(function () {
        var sHeight = $(win).height() - width;
        $(id).css({ "height": sHeight }); //设置高度
        $(id).layout(); //重新布局
    });
}

/* 更换主题 */
function changeThemeFun(themeName) {
    var reg = /Themes\/(.*)\/easyui/;

    $("link").each(function () {
        var eThemeUrl = $(this).attr('href').replace(reg, 'Themes/' + themeName + '/easyui');

        $(this).attr('href', eThemeUrl);
    })

    $('iframe').each(function () {
        $(this).contents().find("link").each(function () {
            var eThemeUrl = $(this).attr('href').replace(reg, 'Themes/' + themeName + '/easyui');

            $(this).attr('href', eThemeUrl);
         })
    });

    /*添加cookie，其中expires为几天后过期，path为作用范围*/
    $.cookie('eTheme', themeName, {
        expires: 7,
        path: "/"
    });
};

//TreeGrid
function TreeGrid(obj, options) {
    var TreeGridOption = { fit: true, fitColumns: true, singleSelect: true, rownumbers: true, resizable: true, border: false, toolbar: [{ text: "新增", iconCls: "icon-add", handler: function () { FnAdd(); } }, "-", { text: "修改", iconCls: "icon-edit", handler: function () { FnEdit(); } }, "-", { text: "删除", iconCls: "icon-remove", handler: function () { FnRemove(); } }] };
    $(obj).treegrid(ChangeOptions(TreeGridOption, options));
}
//DataGrid
function DataGrid(obj, options) {
    var DataGridOptions = {
        nowrap: false, fit: true, fitColumns: true, singleSelect: true, rownumbers: true, border: false, pagination: true, resizable: true, toolbar: [{ text: "新增", iconCls: "icon-add", handler: function () { FnAdd(); } }, "-", { text: "修改", iconCls: "icon-edit", handler: function () { FnEdit(); } }, "-", { text: "删除", iconCls: "icon-remove", handler: function () { FnRemove(); } }]
    };
    $(obj).datagrid(ChangeOptions(DataGridOptions, options));
}
//Dialog
function Dialog(obj, options) {
    var DialogOptions = { closed: true, width: 500, height: 400, modal: true, onClose: function () { $('.validatebox-tip').remove(); } };
    $(obj).dialog(ChangeOptions(DialogOptions, options));
}
//LinkButton
function Button(obj, options) {
    var ButtonOptions = { onClick: function () { } };
    ButtonOptions = ChangeOptions(ButtonOptions, options)
    $(obj).bind('click', ButtonOptions.onClick);
    $(obj).linkbutton(ButtonOptions);
}
//Validate
function Validate(obj, options) {
    var ValidateOption = { required: true, missingMessage: "请输入" };
    $(obj).validatebox(ChangeOptions(ValidateOption, options));
}
//Number
function Number(obj, options) {
    var NumberOptions = { required: true, missingMessage: "请输入" }
    $(obj).numberbox(ChangeOptions(NumberOptions, options));
}
//ComboTree
function Combotree(obj, options) {
    var CombotreeOptions = { missingMessage: "", width: 150, onShowPanel: function () { $(this).combotree("reload"); } };
    $(obj).combotree(ChangeOptions(CombotreeOptions, options));
}
//Combobox
function Combobox(obj, options) {
    var ComboboxOptions = { missingMessage: "", width: 150, valueField: "id", textField: "text", editable: false, onShowPanel: function () { $(this).combobox("reload"); } };
    $(obj).combobox(ChangeOptions(ComboboxOptions, options));
}
//Progress
function Progress(options) {
    var ProgressOptions = { title: "提示", msg: "" };
    $.messager.progress(ChangeOptions(ProgressOptions, options));
}
//Alert
function Alert(options) {
    var AlertOptions = { title: "提示", msg: "", icon: "info", fn: function () { } };
    AlertOptions = ChangeOptions(AlertOptions, options)
    $.messager.alert(AlertOptions.title, AlertOptions.msg, AlertOptions.icon, AlertOptions.fn);
}
//Show
function Show(obj, options) {
    var ShowOptions = { title: "提示", msg: "", timeout: 2000 };
    ShowOptions = ChangeOptions(ShowOptions, options)
    $.messager.alert(ShowOptions.title, ShowOptions.msg, ShowOptions.timeout);
}
//NumberSpinner
function NumberSpinner(obj, options) {
    var NumberSpinnerOptions = { required: true, missingMessage: "请选择", width: 150 };
    NumberSpinnerOptions = ChangeOptions(NumberSpinnerOptions, options);
    $(obj).numberspinner(NumberSpinnerOptions);
}
function DateBox(obj, options) {
    var DateBoxOptions = { editable: false, width: 150 };
    DateBoxOptions = ChangeOptions(DateBoxOptions, options);
    $(obj).datebox(DateBoxOptions);
}
function DateTimeBox(obj, options) {
    var DateTimeBoxOptions = { editable: false, width: 150 };
    DateTimeBoxOptions = ChangeOptions(DateTimeBoxOptions, options);
    $(obj).datetimebox(DateTimeBoxOptions);
}
function Confirm(options) {
    var ConfirmOptions = { title: "提示", msg: "", fn: function (r) { } };
    ConfirmOptions = ChangeOptions(ConfirmOptions, options)
    $.messager.confirm(ConfirmOptions.title, ConfirmOptions.msg, ConfirmOptions.fn);
}
//FormClear
function FormClear(obj) {
    $(obj).form("clear");
}
//AjaxDel
function AjaxDel(options) {
    var AjaxDelOptions = { url: "", data: {}, grid: {}, type: "datagrid", msg: "确定删除吗" };
    AjaxDelOptions = ChangeOptions(AjaxDelOptions, options);
    $.messager.confirm("提示", AjaxDelOptions.msg, function (r) {
        if (r) {
            Progress({ msg: "正在删除..." });
            $.ajax({
                type: "post",
                url: AjaxDelOptions.url,
                data: AjaxDelOptions.data,
                success: function (msg) {
                    $.messager.progress("close");
                    if (msg == "true") {
                        $(AjaxDelOptions.grid)[AjaxDelOptions.type]("reload");
                        Alert({ msg: "删除成功" });
                    }
                    else if (msg != "" && msg != "0" && msg != "false") {
                        Alert({ msg: msg });
                    }
                    else {
                        Alert({ msg: "删除失败" });
                    }
                },
                error: function () {
                    $.messager.progress("close");
                    Alert({ msg: "出错啦", info: "error" });
                }
            });
        }
    });
}
//AjaxUpload
function Upload(btn, path, data, fn) {
    new AjaxUpload(btn, {
        action: '/Ashx/upload.ashx?path=' + path,
        name: "uploadfile",
        data: data,
        onSubmit: function (file, ext) {
            if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))) {
                Alert({ msg: "只能上传 JPG, PNG 或 GIF 类型的文件！" });
                return false;
            }
            Progress({ msg: "正在上传..." });
        },
        onComplete: function (file, response) {
            $.messager.progress("close");
            if (response == "fail") {
                Alert({ msg: "上传失败", icon: "error" });
            } else if (response.indexOf("more2M") > 0) {
                Alert({ msg: "文件大小不得超过2M", icon: "error" });
            }
            else if (response.indexOf('请') > 0) {
                var x = response.indexOf(">");
                var y = response.indexOf("</");
                x = x + 1;
                Alert({ msg: response.toString().substring(x, y), icon: "error" });
            }
            else {
                Alert({ msg: "上传成功" });
                var x = response.indexOf(">");
                var y = response.indexOf("</");
                x = x + 1;
                fn(response.toString().substring(x, y));
            }
        }
    });
}
//弹出新窗口
function NewWindow(url, name, options) {
    var NewWindowOptions = {
        width: screen.availWidth,
        height: screen.availHeight,
        top: (screen.availHeight - screen.availHeight) / 2,
        left: (screen.availWidth - screen.availWidth) / 2,
        toolbar: 0,
        scrollbars: 0,
        menubar: 0,
        resizable: 0,
        location: 0,
        hotkeys: 1
    };
    NewWindowOptions = ChangeOptions(NewWindowOptions, options);
    var param = "";
    for (var key in NewWindowOptions) {
        param += key + "=" + NewWindowOptions[key] + ",";
    }
    window.open(url, name, param);
}
//验证手机号码
function ValidatePhone(phone) {
    var partten = /^1[3,4,5,8]\d{9}$/;
    if (!partten.exec(phone)) {
        return false;
    }
    return true;
}
//ChangeOptions
function ChangeOptions(defaultOptions, options) {
    for (var key in options) {
        defaultOptions[key] = options[key];
    }
    return defaultOptions;
}